package com.alog.yzd.jdbc.hm;

import com.alog.yzd.jdbc.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class JdbcTemplateTest {
    public static void main(String[] args){

    }

    public User findUser(String name, String pwd) throws DataAccessException {
        JdbcTemplate template = new JdbcTemplate(JdbcUtils2.getDataSource());
        String sql = "select id, name, pwd, birth from user where name = ? and pwd = ?";
        Object[] params = new Object[]{name, pwd};
        User user2 = null;

//        user = template.queryForObject(sql, params, new RowMapper<User>() {
//
//            @Override
//            public User mapRow(ResultSet rs, int i) throws SQLException {
//                User user = new User();
//                user.setId(rs.getInt("id"));
//                user.setBirth(rs.getDate("birth"));
//                user.setPwd(rs.getString("pwd"));
//                user.setName(rs.getString("name"));
//                return user;
//            }
//        });

        user2 = template.queryForObject(sql, params, (rs, i)->{
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setBirth(rs.getDate("birth"));
            user.setPwd(rs.getString("pwd"));
            user.setName(rs.getString("name"));
            return user;
        });
        return user2;
    }
}
